$().ready(function() {
	get_hash();
	$('#submitSettings').click(function(){
		saveSettings();
	});
});
function list_settings(){
	var label;
	var asort = 0;
	settings_icons = {};
	xfunc = "system_settings/list_system_settings";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					if(result.intID[k]=='1'){
						label = "<td class='system_label'>Admin E-mail Address</td>";
					} else if(result.intID[k]=='2') {
						label = "<td class='system_label'>Admin Name</td>";
					}/* else if(result.intID[k]=='3') {
						label = "<td class='system_label'>Admin Message</td>";
					}*/else if(result.intID[k]=='4') {
						label = "<td class='system_label'>Company Name</td>";
					} else {
						label = "<td class='system_label'>APP Name</td>";
					}
					if(result.intID[k]!='3'){
					settings_icons_panel = '';
						settings_icons_panel +='<tr>';
							settings_icons_panel += label;
								settings_icons_panel +='<td>'+result.strValue[k]+'</td>';
							settings_icons_panel +='<td class="controls"><div class="btn-group">';
							settings_icons_panel +='<button class="btn btn-primary" onclick="javascript:editSettings(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							//settings_icons_panel +='<button class="btn btn-danger" onclick="javascript:delSettings(\''+result.intID[k]+'\', \''+result.strValue[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							settings_icons_panel +='</td>';
						settings_icons_panel +='</tr>';
						settings_icons[result.intID[k]]=settings_icons_panel;
					}

				});
				settings = '<table class="table table-striped table-bordered">';
				settings += '<thead>';
					settings += '<tr>';
						settings += '<th>System Settings</th>';
						settings += '<th>&nbsp;</th>';
						settings += '<th>Control</th>';
					settings += '</tr>';
				settings += '</thead>';
				settings += '<tbody>';
				for(x=0;x<=asort;x++){
					if($.trim(settings_icons[x])!=""){
						settings +=settings_icons[x];
					}	
				}		
				settings += '</tbody>';		
				settings += '</table>';
				
				$('#settings_content').html(settings);
			}
		});	
}
function editSettings(intID){
	xfunc = "system_settings/get_system_settings";
	data_sent = {
			'intID':intID
	};
	var x;
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			if(result.strVariable=="strAdminEmail"){
				x = "Admin Email";
			} else if(result.strVariable=="strAdminEmailName"){
				x = "Admin Name";
			} else if(result.strVariable=="strSentPasswordEmail"){
				x = "Admin Message";
			} else if(result.strVariable=="strCompanyName"){
				x = "Company Name";
			} else if(result.strVariable=="strAppName"){
				x = "APP Name";
			} else {
				//do nothing
			}
			$("#intSettingsID").val(intID);
			$("#strVarSetting").html(x);
			$("#strValue").val(result.strValue);
		}
	});
	
	$("#settings_content").hide();
	$("#editSettings").slideDown('slow', function(){
		$(".back-btn-settings a").attr("onclick","btn_back_settings()");
	});		
}
function saveSettings(){
	var intID = $("#intSettingsID").val();
	var strValue = $("#strValue").val();
	xfunc = "system_settings/save_settings";
	data_sent = {
			'intID':intID,
			'strValue':strValue
	};
	
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#settingsSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delSettings(intID, titleName){
	xfunc = "system_settings/delete_settings";
	data_sent = {
			'intID':intID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				list_settings();
			}
		});
    });
}
function btn_back_settings(){
	$("#editSettings").hide();
	$("#settings_content").slideDown('slow', function(){
		list_settings();
	});	
}
